package com.roger.cmdkit;

import com.roger.cmdkit.base.CmdBase;
import com.roger.cmdkit.base.CmdHelper;
import com.roger.cmdkit.base.CmdSet;
import com.roger.cmdkit.info.Error;
import com.roger.cmdkit.info.Help;

public class CmdMain {
    private static final String TAG = CmdMain.class.getSimpleName();
    private static String[] cmdSets={
            "-help","-version",
            "-bill",
            "-dock",
            "-bug", "-path", "-today",
            "-c",
            "-d",
            "-E","-D",
            "-h","-w",
            "--test-cmd"};

    public static void main(String... args) {
        runArgs(getArgs(args));
    }

    private static String[] getArgs(String... args) {
        String[] args1;
        if (args.length>0){
            if(CmdHelper.isBelongArgs(cmdSets,args[0])){
                args1 = new String[args.length];
                System.arraycopy(args, 0, args1, 0, args1.length);
            }else{
                args1 = new String[100];
                args1[0]=args[0];
            }
        }else{
            args1 = new String[0];
        }
        return args1;
    }

    private static void runArgs(String... args) {
        try {
            CmdBase cmd;
            if (args.length == 0) cmd = new Help();
            else if(args.length == 100) cmd=new Error();
            else cmd = CmdSet.builder(args[0]);
            cmd.cmdS(args);
        }  catch (Throwable e) {
            System.out.println(TAG + ": run args fail");
        }
        System.exit(0);
    }


}
